import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		isLogin: uni.getStorageSync('token') ? true : false,
		token: uni.getStorageSync('token') || "",
		userInfo: uni.getStorageSync('userinfo') || '',
		adjustPosition: true,
		onlineMessageId: ''
	},
	getters: {
		// 是否登录
		hasLogin(state) {
			return !!state.token;
		},
	},
	mutations: {
		login(state, val) {
			state.isLogin = true;
		},
		ADJUST_POSITION(state, val) {
			state.adjustPosition = val;
			console.log('%c [ state.adjustPosition ]-23', 'font-size:13px; background:pink; color:#bf2c9f;', state.adjustPosition)
		},
		ONLINE_MESSAGE_ID(state, val) {
			state.onlineMessageId = val;
			console.log('%c [ state.adjustPosition ]-23', 'font-size:13px; background:pink; color:#bf2c9f;', state.adjustPosition)
		},
		changeToken(state, val) {
			state.token = val;
		},
		//更新token
		setToken(state, data) {
			const {
				token,
				userInfo,
				uid,
				userID,
				userSig
			} = data;
			state.isLogin = true;
			// state.token = token;
			state.userInfo = userInfo;

			// uni.setStorageSync("token", token);
			// uni.setStorageSync("userInfo", userInfo);
			uni.setStorageSync("userID", userID);
			uni.setStorageSync("userSig", userSig);
			this.dispatch("getUserInfo"); //更新用户信息
		},
		//退出登录
		logout(state) {
			console.log('%c [ 退出登录方法 ]-46', 'font-size:13px; background:pink; color:#bf2c9f;')
			state.token = "";
			state.userInfo = "";
			state.isLogin = false;
			uni.removeStorageSync('userID')
			uni.removeStorageSync('userSig')
			uni.removeStorageSync('token')
			uni.removeStorageSync('mobile')
			uni.removeStorageSync('mobile2')
			uni.removeStorageSync('userinfo')
			uni.removeStorageSync('userInfo')
			setTimeout(() => {
				state.userInfo = {};
			}, 1100);
		},
		//更新state的数据
		setStateAttr(state, param) {
			if (param instanceof Array) {
				for (let item of param) {
					state[item.key] = item.val;
				}
			} else {
				state[param.key] = param.val;
			}
		},
		//更新state obj对象的数据
		setStateObj(state, param) {
			for (let item of param) {
				state[item.obj][item.key] = item.val;
			}
		},
	},
	actions: {
		//更新用户信息
		async getUserInfo({
			state,
			commit
		}) {},
	},
});

export default store;